﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Sharp.Infrastructure
{
    /// <summary>
    /// 集合类
    /// </summary>
    public class MyCollections
    { 

        #region 去除数组的相同项,去除数组中的空项
        /// <summary>
        /// 去除数组的相同项,去除数组中的空项
        /// </summary>
        /// <param name="input">可以被转换成数组的字符串</param>
        /// <param name="pattern">分割符号</param>
        /// <returns></returns>
        public static List<string> NoSomeValue(string input, char pattern)
        {
            string[] arrTemp = input.Split(pattern);

            return NoSomeValue(arrTemp);
        }
        /// <summary>
        /// 去除数组的相同项,去除数组中的空项
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static List<string> NoSomeValue(List<string> input)
        {
            return NoSomeValue(input.ToArray());
        }

        /// <summary>
        /// 去除数组的相同项,去除数组中的空项
        /// </summary>
        /// <param name="input">传入的数组</param>
        /// <returns>返回ArrayList</returns>
        public static List<string> NoSomeValue(string[] input)
        {
            List<string> list = new List<string>();
            if (input.Length > 0)
            {
                Array.Sort(input);

                int size = 1;
                for (int i = 1; i < input.Length; i++)
                    if (input[i] != input[i - 1])
                        size++;

                String[] myTempData = new String[size];

                int j = 0;

                myTempData[j++] = input[0];

                for (int i = 1; i < input.Length; i++)
                    if (input[i] != input[i - 1])
                        myTempData[j++] = input[i];


                list.AddRange(myTempData);
                return list;
            }

            list.AddRange(input);
            return list;
        }



        /// <summary>
        /// 去除数组的相同项 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static IEnumerable<T> NoSomeValue<T>(IEnumerable<T> input)
        { 
            List<T> list = new List<T>(); 
            foreach (T temp in input)
            {
                if (list.Contains(temp) == false)
                {
                    list.Add(temp);
                }
            }
            return list;
        }
        #endregion

        #region 倒序
        /// <summary>
        /// 倒序
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string[] Reverse(string[] str)
        {
            int len = str.Length;
            for (int i = 0; i < len / 2; i++)
            {
                string temp = str[i];
                str[i] = str[len - 1 - i];
                str[len - 1 - i] = temp;
            }

            return str;
        }
        #endregion

        /// <summary>
        /// 是否包含输入的值
        /// </summary>
        /// <param name="orginal"></param>
        /// <param name="matches"></param>
        /// <returns></returns>
        /// <remarks>用于取代大量的 if 代码。</remarks>
        static public bool Contain(string orginal, params string[] matches)
        {
            if (orginal == null)
            {
                return false;
            }
            return matches.Any(t => orginal.IndexOf(t, StringComparison.CurrentCultureIgnoreCase) > -1);
        }
        /// <summary>
        /// 是否包含输入的值
        /// </summary>
        /// <param name="orginal"></param>
        /// <param name="matches"></param>
        /// <returns></returns>
        /// <remarks>用于取代大量的 if 代码。</remarks>
        static public bool Contain(string orginal, IEnumerable<string> matches)
        {
            if (orginal == null)
            {
                return false;
            }
            return matches.Any(t => orginal.IndexOf(t, StringComparison.CurrentCultureIgnoreCase) > -1);
        }
    
    }
}